package com.test.particles;

import java.util.Iterator;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import javax.vecmath.Point3f;

import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;

public class OpenGLRenderer implements Renderer {
	OpenGLRenderer(ParticleSystem particles)
	{
		mParticles = particles;
	}
	
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// Set the background color to black ( rgba ).
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);  // OpenGL docs.
		// Enable Smooth Shading, default not really needed.
		gl.glShadeModel(GL10.GL_SMOOTH);// OpenGL docs.
		// Depth buffer setup.
		gl.glClearDepthf(1.0f);// OpenGL docs.
		// Enables depth testing.
		gl.glEnable(GL10.GL_DEPTH_TEST);// OpenGL docs.
		// The type of depth testing to do.
		gl.glDepthFunc(GL10.GL_LEQUAL);// OpenGL docs.
		// Really nice perspective calculations.
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, // OpenGL docs.
                          GL10.GL_NICEST);
		
		mCircle = new Circle();
	}

	public void onDrawFrame(GL10 gl) {
		// Clears the screen and depth buffer.
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		
		// Replace the current matrix with the identity matrix
		
//        float offset = 0.1f * ((mills % 1000) / 1000.0f - 0.5f);
        
//        gl.glRotatef(360.0f * ((mills/1000.0f) % 1.0f), 0, 0, 1);
		// Translates 4 units into the screen.
		
		mParticles.step();
		
		Iterator<Particle> particleIt = mParticles.getParticles().iterator();
		
        while(particleIt.hasNext())
        {
        	Particle currentParticle = particleIt.next();
        	
        	float drawPosX = 2.0f * (currentParticle.getPos().x - 0.5f);
        	float drawPosY = 2.0f * currentParticle.getPos().y - (float) mHeight / mWidth;
        	
			gl.glLoadIdentity();
			gl.glTranslatef(drawPosX, drawPosY, -4);
			
			Point3f particleColor = currentParticle.getColor();
			
	        gl.glColor4f(particleColor.x,particleColor.y,particleColor.z,1.0f);
	        
			mCircle.draw(gl);
        }
	}

	public void onSurfaceChanged(GL10 gl, int width, int height) {
		// Sets the current view port to the new size.
		gl.glViewport(0, 0, width, height);// OpenGL docs.
		// Select the projection matrix
		gl.glMatrixMode(GL10.GL_PROJECTION);// OpenGL docs.
		// Reset the projection matrix
		gl.glLoadIdentity();// OpenGL docs.
		// Calculate the aspect ratio of the window
//		GLU.gluOrtho2D(gl, 0.0f, 1.0f, (float) height / width, 0.0f)
		GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f);
		// Select the modelview matrix
		gl.glMatrixMode(GL10.GL_MODELVIEW);// OpenGL docs.
		// Reset the modelview matrix
		gl.glLoadIdentity();// OpenGL docs.
		
		mWidth = width;
		mHeight = height;
	}
	
//	public void setPosition(float posX, float posY, int width, int height) {
//		mPosX = ((posX / width) - 0.5f) * 2;
//		mPosY = -((float) height / width * 2 * ((posY/height) - 0.5f));
//	}
	
	private int mWidth;
	private int mHeight;
	
	private float mPosX;
	private float mPosY;
	
	private Circle mCircle;
	
	private ParticleSystem mParticles;
}
